package com.chehejia.devmng.platform.base.model.page;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * @author SongJian
 * Created by SongJian at 2018/3/28.
 */
@Setter
@Getter
@NoArgsConstructor
@AllArgsConstructor
@Builder
@ApiModel("分页参数")
public class PageParam<E> implements Serializable {

    private static final long serialVersionUID = 2577173396350163855L;

    private E param;

    /**
     * 查询页码
     */
    @NotNull
    @Min(value = 1, message = "查询页码必须大于等于1")
    @ApiModelProperty("查询页码")
    private int pageNo = 1;

    /**
     * 页大小
     */
    @NotNull
    @Min(value = 1,message = "分页大小必须大于等于1")
    @ApiModelProperty("分页大小")
    private int pageSize = 30;

    /**
     * 排序字段
     */
    @ApiModelProperty("排序字段")
    private String orderBy;

    /**
     * 是否升序-默认是降序
     */
    @ApiModelProperty("是否升序")
    private boolean asc;

    public <T> PageParam<T> convert(T param) {
        return PageParam.<T>builder()
                .pageNo(getPageNo())
                .pageSize(getPageSize())
                .asc(this.isAsc())
                .orderBy(getOrderBy())
                .param(param)
                .build();
    }
}
